﻿using AutoMapper;

namespace CobraCms.MsSqlContentRepository.Data
{
    public interface IConvertable<TData, TEntity> where TEntity : class, new()
    {
        PhosphorConverter<TData, TEntity> Converter { get; }
    }
	public class PhosphorConverter<TData, TEntity> where TEntity : class, new()
	{
		private readonly TData dataObject;

		public PhosphorConverter(TData dataObject)
		{
			this.dataObject = dataObject;
		}

		public TEntity ToEntity()
		{
			return ToEntity(new TEntity());
		}

		public TEntity ToEntity(TEntity target)
		{
			target = Mapper.Map(dataObject, target);
			return target;
		}

		public TData FromEntity(TEntity entity)
		{
			if (entity == null)
			{
				return dataObject;
			}

			Mapper.Map(entity, dataObject);
			return dataObject;
		}
	}

}
